<?php

namespace App\Http\Controllers;

use App\Library\Lock;
use App\Library\Requests\Base\BasePageRequest;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Coupon\CouponPageRequest;
use App\Library\Service\CouponService;
use Illuminate\Http\JsonResponse;

class CouponController extends BaseController
{
    /**
     * @param CouponService $service
     */
    public function __construct(CouponService $service)
    {
        $this->service = $service;
    }

    /**
     * 优惠券领取列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     */
    public function paging(CouponPageRequest $request): JsonResponse
    {
        return $this->service->paging($request);
    }

    /**
     * 优惠券领取列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     *
     */
    public function claimedPaging(CouponPageRequest $request): JsonResponse
    {
        return $this->service->claimedPaging($request);
    }

    /**
     * 用户领取优惠券
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function claimCoupon(SerialNumberRequest $request): JsonResponse
    {
        return Lock::get(__CLASS__ . __FUNCTION__ . $request->serialNumber, function () use ($request) {
            return $this->service->claimCoupon($request);
        });
    }
    /**
     * 我的优惠券列表
     * @param CouponPageRequest $request
     * @return JsonResponse
     */
    public function myPaging(CouponPageRequest $request): JsonResponse
    {
        return $this->service->myPaging($request);
    }

    /**
     * 领取优惠券
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function receive(SerialNumberRequest $request): JsonResponse
    {
        return Lock::get(__CLASS__ . __FUNCTION__ . $request->serialNumber, function () use ($request) {
            return $this->service->receive($request);
        });
    }

    /**
     * 未使用优惠券数量
     * @return JsonResponse
     */
    public function noUseCount(): JsonResponse
    {
        return $this->service->noUseCount();
    }
}
